/*
* @(#)WorkflowEditor
*
* Copyright (c) 2004 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.workflow.applet;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;

import com.dcivision.dms.client.HtmlFormText;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.Utility;
import com.dcivision.workflow.applet.editor.Editor;
import com.dcivision.workflow.applet.net.HttpOperation;
import com.dcivision.workflow.bean.WorkflowGroup;
import com.dcivision.workflow.bean.WorkflowRecord;

/**
 * <p>Class Name:       WorkflowEditor.java    </p>
 * <p>Description:      The applet workflow editor for workflow </p>
 *
 * @author           Beyond Qu
 * @company          DCIVision Limited
 * @creation date    01/09/2004
 * @version          $Revision: 1.17 $
 */

public class WorkflowEditor extends JApplet  implements DropTargetListener{

  public static final String REVISION = "$Revision: 1.17 $";

  private Container contentPane ; 
  private JScrollPane sGraph ;
  private JScrollPane sTree  =new JScrollPane(); 
  private JSplitPane pane   ;
  private DragTrees tree ;
  private Editor editor;
  
  private String hostURL ="";
  private String loginName ="";
  private String loginPsw ="";
  private String locale ="";
  private String loginURL = "/Login.do";
  private String sessionID = "";
  URL baseURL;
  String projectName;
  private String getRecordGroupDataURL = "/workflow/appletMiddleware?dataType=GET_RECORDGROUP";
  private String getResouceURL = "/workflow/appletMiddleware?dataType=RESOURCE";
  private String getStepDataURL = "/workflow/appletMiddleware?dataType=GET_WORKFLOWSTEP&workflowRecordID=";  
  private String maintWorkflowRecordURL = "/workflow/MaintWorkflowRecord.do?fromType=A";
  
  
  private HttpOperation httpOperation = new HttpOperation(this);
  
  private JPopupMenu treePopupMenu = new JPopupMenu();

  private JMenuItem jMenuItem1 = new JMenuItem();  
  private JMenuItem jMenuItem2 = new JMenuItem();  
  private JMenuItem jMenuItem3 = new JMenuItem();
  private JMenuItem jMenuItem4 = new JMenuItem();  
  private JMenuItem jMenuItem5 = new JMenuItem();  
  
  
  private String currentSelectedTreeNodeType = "";
  private TreeUseWorkflowGroup  currentSelectedTreeNodeWorkflowGroup ;
  private TreeUseWorkflowRecord currentSelectedTreeNodeWorkflowRecord ;
  WorkflowRecordInfoWindow workflowRecordInfoWindow;
  
  /**
   * init()
   * Applet init() method
   *
   * 
   */  
  public void init(){
/*    
    // windows look style
    try {
        UIManager.setLookAndFeel(
                 "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception e) {}    
*/  
    try{  
    WindowUtilities.setUIFont(new javax.swing.plaf.FontUIResource("SansSerif", 0, 12));
    //hostURL = getParameter("hostURL");
    String debug = getParameter("DEBUG");
    String urlString ;
    if("1".equals(debug)){
      urlString="http://localhost:8080/eip/login.do";
      baseURL = new URL("http://localhost:8080");
    } else {      
      urlString  = this.getDocumentBase().toString();
      baseURL = getCodeBase();
    }
    //System.out.println(baseURL);
    projectName = WindowUtilities.splitString(urlString,"/")[3];
    String[] urlAry = WindowUtilities.splitString(urlString,"/");
    projectName = urlAry[3];
    hostURL=urlAry[0]+"/"+urlAry[1]+"/"+urlAry[2]+"/"+urlAry[3];
    httpOperation.setBaseURL(baseURL);
    httpOperation.setProjectName(projectName);
    loginName = getParameter("loginName");
    loginPsw = getParameter("loginPsw");
    loginPsw = Crypt.decrypt(loginPsw, "dcivision");
    locale = getParameter("locale");
    sessionID = getParameter("sessionID");
    //System.out.println(hostURL);
    loginURL = hostURL+"/Login.do";
    //getRecordGroupDataURL = hostURL + getRecordGroupDataURL;
    ObjectInputStream inputFromServlet = null;
    //getStepDataURL = hostURL + getStepDataURL;
    setSize(800, 600);
    java.util.Locale newLocale = Utility.getLocaleByString(getParameter(GlobalConstant.LANGUAGE_PARAMETER));
    //getSession().setAttribute(org.apache.struts.Globals.LOCALE_KEY, newLocale);
    //login first
    
      if (!httpOperation.login(baseURL,loginName,loginPsw,sessionID)){
        JOptionPane.showInternalMessageDialog(this,"Login failed!","Message",JOptionPane.INFORMATION_MESSAGE);
        this.destroy();
      }
      //getAppletContext().showDocument(new URL("http://zealot:8080/eip/dms/ListDmsFolderSelect.do?rootID=10&parentID=606&formName=maintWorkflowRecordForm&displayFieldName=locationPath&hiddenRootIDFieldName=rootID&hiddenParentIDFieldName=parentID&allowCompoundDoc=false&allowPaperDoc=false"),"_blank");
      //String sessionId = getCookie("SESSIONID");
      // get group list form ParaDM system  
      inputFromServlet = httpOperation.getObjectInputStreamFromServlet(getResouceURL+"&LOCALE="+locale);
      Object obj = null;
      obj = inputFromServlet.readObject();
      String[] resourceLable=null;

      //group list
      if (obj instanceof String[]) {
        AppletResource.lableArr = (String[]) obj;
      } else if (obj instanceof Exception) {
          //otherwise type cast it to Exception and throw that exception.
          throw (Exception) obj;
      }           
    }catch (Exception e){
        JOptionPane.showInternalMessageDialog(this,"Get resource information failed!","Message",JOptionPane.INFORMATION_MESSAGE);        
        return;
    
    }
    
    // init jumpMenu
    
    jMenuItem1.setText(AppletResource.lableArr[0]);
    jMenuItem2.setText(AppletResource.lableArr[1]);
    jMenuItem3.setText(AppletResource.lableArr[2]);   
    jMenuItem4.setText(AppletResource.lableArr[3]);
    treePopupMenu.add(jMenuItem1);
    treePopupMenu.add(jMenuItem2);
    treePopupMenu.add(jMenuItem3);  
    treePopupMenu.add(jMenuItem5);    
    treePopupMenu.add(jMenuItem4);    
    
    // new Group menu
    jMenuItem1.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){
            createWorkflowRecordInfoWindow("N");
        }            
    });    
    
    // edit Group menu    
    jMenuItem2.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){
            createWorkflowRecordInfoWindow("E");
        }            
    });
    // editor Group menu
    jMenuItem3.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){
            //createWorkflowRecordInfoWindow("V");
          editor.closeEditor();
          editor.setWorkflowRecordID(currentSelectedTreeNodeWorkflowRecord.getID());
          editor.setIspending("P".equals(currentSelectedTreeNodeWorkflowRecord.getStatus())?true:false);
          //editor.setWorkflowStepList(getWorkflowStepList());                    
          editor.initEditor();
          editor.setEnabled(true);
            
        }            
    });
    // delete Group menu    
    jMenuItem4.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){
            deleteWorkflowRecord();
            reloadTree();
        }            
    }); 
    
    contentPane = getContentPane();
    
    // Construct Frame
    editor = new Editor(hostURL);
    new DropTarget(editor.getGraph(), 
        DnDConstants.ACTION_COPY_OR_MOVE,
        this);
    editor.setHttpOperation(httpOperation);
    
    sGraph = new JScrollPane(editor);

    // create the group tree
    tree = createGroupTree();
    tree.addMouseListener( new MouseAdapter(){
        public void mousePressed(MouseEvent e){
            if(SwingUtilities.isRightMouseButton(e)){ 
                treePopupMenu.show(tree,e.getX(),e.getY()) ;
            }
        }    
    });
    sTree.getViewport().add(tree,null);
    sTree.setBackground(Color.BLUE);
    //sTree = new JScrollPane(tree);
    
    
    contentPane.setLayout(new BorderLayout());
    pane =
       new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sGraph, sTree);
    pane.setContinuousLayout(true); 
    pane.setOneTouchExpandable(true);   
    pane.setDividerLocation(650);         
    contentPane.add(pane, BorderLayout.CENTER);    
    contentPane.add(editor.createToolBar(), BorderLayout.NORTH);
    editor.setEnabled(false);
  }
  
  /**
   * createGroupTree
   *
   * Create Group Tree ,it's workflow record and operate Menu   
   *
   * @return   The Group tree
   */
  private DragTrees createGroupTree(){    
    DragTrees tempTree ;
    DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
    ObjectInputStream inputFromServlet = null;    
    
    // root
    DefaultMutableTreeNode root =
      new DefaultMutableTreeNode(AppletResource.lableArr[4]);    
     
    DefaultMutableTreeNode child;
    DefaultMutableTreeNode grandChild;

    List workflowGroupList = new ArrayList();
    List workflowRecordList = new ArrayList();
    URL openUrl = getURLByString("gif/icon_folderopen.gif");        
    Icon open = new ImageIcon(openUrl);
    URL closeUrl = getURLByString("gif/icon_folderclose.gif");        
    Icon close = new ImageIcon(closeUrl);
    URL fileUrl = getURLByString("gif/icon_file.gif");        
    Icon file = new ImageIcon(fileUrl);
    
    renderer.setClosedIcon(close);
    renderer.setOpenIcon(open);
    renderer.setLeafIcon(file);
    renderer.setOpaque(false);
    renderer.setBackground(null);
    renderer.setBackgroundNonSelectionColor(null);
    //renderer.setBackgroundSelectionColor(null);
    try{
      // get group list form ParaDM system  
      inputFromServlet = httpOperation.getObjectInputStreamFromServlet(getRecordGroupDataURL);
      Object obj = null;
      obj = inputFromServlet.readObject();

      //group list
      if (obj instanceof List) {
          workflowGroupList = (List) obj;
      } else if (obj instanceof Exception) {
          //otherwise type cast it to Exception and throw that exception.
          throw (Exception) obj;
      }
      
      // workflow record list
      obj = inputFromServlet.readObject();      
      if (obj instanceof List) {
          workflowRecordList = (List) obj;
      } else if (obj instanceof Exception) {
          //otherwise type cast it to Exception and throw that exception.
          throw (Exception) obj;
      }
      
    }catch (Exception e){
        JOptionPane.showInternalMessageDialog(this,"Get tree information failed!","Message",JOptionPane.INFORMATION_MESSAGE);        
        return null;
    
    }
    
    //construct  the tree    
    for(int childIndex=0; childIndex<workflowGroupList.size() ; childIndex++) {
      WorkflowGroup workflowGroup = (WorkflowGroup)workflowGroupList.get(childIndex);
      TreeUseWorkflowGroup treeUseWorkflowGroup = new TreeUseWorkflowGroup();
      treeUseWorkflowGroup.setID(workflowGroup.getID());
      treeUseWorkflowGroup.setGroupName(workflowGroup.getGroupName());      
      treeUseWorkflowGroup.setCreatorID(workflowGroup.getCreatorID());
      child = new DefaultMutableTreeNode(treeUseWorkflowGroup);
      //child = new DefaultMutableTreeNode(workflowGroup.getGroupName());
      root.add(child);        
      for(int grandChildIndex=0; grandChildIndex<workflowRecordList.size(); grandChildIndex++) {
        WorkflowRecord workflowRecord = (WorkflowRecord)workflowRecordList.get(grandChildIndex);
        if(workflowRecord.getWorkflowGroupID().intValue()  == treeUseWorkflowGroup.getID().intValue() ){
          //grandChild =
            //new DefaultMutableTreeNode(workflowRecord.getWorkflowCode());
          TreeUseWorkflowRecord treeUseWorkflowRecord = new TreeUseWorkflowRecord();
          treeUseWorkflowRecord.setID(workflowRecord.getID());
          treeUseWorkflowRecord.setWorkflowCode(workflowRecord.getWorkflowCode());
          treeUseWorkflowRecord.setWorkflowCategory (workflowRecord.getWorkflowCategory() );
          treeUseWorkflowRecord.setWorkflowCategoryID (workflowRecord.getWorkflowCategoryID());
          treeUseWorkflowRecord.setWorkflowGroupID (workflowRecord.getWorkflowGroupID());
          treeUseWorkflowRecord.setWorkflowVersion(workflowRecord.getWorkflowVersion());
          treeUseWorkflowRecord.setStartDate(workflowRecord.getStartDate());
          treeUseWorkflowRecord.setEndDate(workflowRecord.getEndDate());
          treeUseWorkflowRecord.setDescription(workflowRecord.getDescription());
          treeUseWorkflowRecord.setCreateDate(workflowRecord.getCreateDate() );
          treeUseWorkflowRecord.setCreatorID(workflowRecord.getCreatorID());
          treeUseWorkflowRecord.setCreatorName(workflowRecord.getCreatorName());
          treeUseWorkflowRecord.setUpdateCount(workflowRecord.getUpdateCount() );
          treeUseWorkflowRecord.setUpdateDate(workflowRecord.getUpdateDate());
          treeUseWorkflowRecord.setUpdaterID(workflowRecord.getUpdaterID());
          treeUseWorkflowRecord.setUpdaterName(workflowRecord.getUpdaterName());
          treeUseWorkflowRecord.setRecordStatus(workflowRecord.getRecordStatus());
          treeUseWorkflowRecord.setStatus(workflowRecord.getStatus());
          
          
          grandChild =
            new DefaultMutableTreeNode(treeUseWorkflowRecord);            
          child.add(grandChild);
        } 
      }
    }
    
    tempTree = new DragTrees(root);
    tempTree.setCellRenderer(renderer);

    tempTree.addTreeSelectionListener(
        new TreeSelectionListener(){
            public void valueChanged(TreeSelectionEvent e){
                TreePath path = e.getNewLeadSelectionPath();     
                if (path == null){
                    return;    
                }
                path.getParentPath();
                Object obj = path.getLastPathComponent() ;
                 
                if( ((DefaultMutableTreeNode)obj).getUserObject() instanceof TreeUseWorkflowRecord){
                    TreePath tempPath = path.getParentPath();
                    Object obj1 = tempPath.getLastPathComponent() ;
                    setCurrentSeletedTreeNode("record",
                            (TreeUseWorkflowGroup)((DefaultMutableTreeNode)obj1).getUserObject(),
                                (TreeUseWorkflowRecord)((DefaultMutableTreeNode)obj).getUserObject());    
                }else if( ((DefaultMutableTreeNode)obj).getUserObject() instanceof TreeUseWorkflowGroup){
                    setCurrentSeletedTreeNode("group",(TreeUseWorkflowGroup)((DefaultMutableTreeNode)obj).getUserObject(),null);                        
                }else {
                    setCurrentSeletedTreeNode("root",null,null);                        
                }
            }
        }
    );
    
    tempTree.addMouseListener( new MouseAdapter(){
        public void mousePressed(MouseEvent e){
            if(SwingUtilities.isRightMouseButton(e)){ 
                treePopupMenu.show(tree,e.getX(),e.getY()) ;
            }else if(SwingUtilities.isLeftMouseButton(e)&&e.getClickCount()==2){                
                // is root
                if(currentSelectedTreeNodeWorkflowRecord == null && currentSelectedTreeNodeWorkflowGroup == null){
                }
                // not workflow record    
                else if(currentSelectedTreeNodeWorkflowRecord == null){
                }
                // workflow record
                else
                {
                    editor.closeEditor();
                    editor.setWorkflowRecordID(currentSelectedTreeNodeWorkflowRecord.getID());
                    editor.setIspending("P".equals(currentSelectedTreeNodeWorkflowRecord.getStatus())?true:false);
                    //editor.setWorkflowStepList(getWorkflowStepList());                    
                    editor.initEditor();
                    editor.setEnabled(true);
                }                
            }
        }    
         
    });
        
    return tempTree;
  
  } 

  /**
   * setCurrentSeletedTreeNode
   *
   * set current seleted TreeNode's object   
   * @param selectedType
   * @param treeUseWorkflowGroup
   * @param treeUseWorkflowRecord 
   * 
   */  
  private void setCurrentSeletedTreeNode(String selectedType ,TreeUseWorkflowGroup treeUseWorkflowGroup
        ,TreeUseWorkflowRecord treeUseWorkflowRecord){    
    currentSelectedTreeNodeType = selectedType ;
    currentSelectedTreeNodeWorkflowGroup   = treeUseWorkflowGroup;
    currentSelectedTreeNodeWorkflowRecord  = treeUseWorkflowRecord;  
    changeMenuStatusByNode();
  }

  /**
   * createWorkflowRecordInfoWindow
   * create and open WorkflowRecordInfoWindow   
   * @param opType
   * 
   */    
  private void createWorkflowRecordInfoWindow(String opType){
    Integer currentGrouID = new Integer(0);
    if(currentSelectedTreeNodeWorkflowGroup != null)
        currentGrouID = currentSelectedTreeNodeWorkflowGroup.getID();    
    workflowRecordInfoWindow = new WorkflowRecordInfoWindow(httpOperation,hostURL,currentGrouID);
    workflowRecordInfoWindow.setParentWindow(this);
    workflowRecordInfoWindow.setNavMode(opType);
    if("E".equals(opType)||"V".equals(opType)){
        workflowRecordInfoWindow.setOpMode("UPD");
        workflowRecordInfoWindow.setTreeUseWorkflowRecord(currentSelectedTreeNodeWorkflowRecord);
        workflowRecordInfoWindow.initRecordInfo();
    }    
    
    workflowRecordInfoWindow.setBounds(WorkflowEditor.this.getX(),WorkflowEditor.this.getY(),450,450);
    workflowRecordInfoWindow.setLocationRelativeTo(WorkflowEditor.this);
    workflowRecordInfoWindow.setModal(true);
    workflowRecordInfoWindow.show() ;    
  }

  /**
   * deleteWorkflowRecord
   * Delete WorkflowRecord
   * 
   */     
  public void deleteWorkflowRecord(){
    try{
        httpOperation.reset() ;
        httpOperation.setSubmissionURL(maintWorkflowRecordURL);
        httpOperation.addHtmlFormText(new HtmlFormText("opMode", "DEL" ));
        httpOperation.addHtmlFormText(new HtmlFormText("navMode", "" ));
        httpOperation.addHtmlFormText(new HtmlFormText("ID", currentSelectedTreeNodeWorkflowRecord.getID().toString()  ));
        httpOperation.addHtmlFormText(new HtmlFormText("recordStatus",
                currentSelectedTreeNodeWorkflowRecord.getRecordStatus().toString()  ));
        httpOperation.addHtmlFormText(new HtmlFormText("updateCount",
                currentSelectedTreeNodeWorkflowRecord.getUpdateCount().toString()  ));
        httpOperation.addHtmlFormText(new HtmlFormText("creatorID",
                currentSelectedTreeNodeWorkflowRecord.getCreatorID().toString()  ));
        httpOperation.addHtmlFormText(new HtmlFormText("createDate",
                currentSelectedTreeNodeWorkflowRecord.getCreateDate().toString()  ));    
        httpOperation.addHtmlFormText(new HtmlFormText("updaterID",
                currentSelectedTreeNodeWorkflowRecord.getUpdaterID().toString()  ));
        httpOperation.addHtmlFormText(new HtmlFormText("updateDate",
                currentSelectedTreeNodeWorkflowRecord.getUpdateDate().toString()  ));    
        
        String returnValue = httpOperation.transmit().toString();
        returnValue = returnValue.replaceAll("\n","");
        
        Editor editor = this.getEditor();
        editor.outputMessage(returnValue, false);
    }catch(Exception e){
        this.destroy();
        System.out.println(e.toString()) ;            
    }       
  }

  /**
   * changeMenuStatusByNode
   * Change menu's status by selected node
   * 
   */   
  public void changeMenuStatusByNode(){
    // is root
    if(currentSelectedTreeNodeWorkflowRecord == null && currentSelectedTreeNodeWorkflowGroup == null){
        jMenuItem1.setEnabled(false);
        jMenuItem2.setEnabled(false);
        jMenuItem3.setEnabled(false);
        jMenuItem4.setEnabled(false);        
    }
    // not workflow record    
    else if(currentSelectedTreeNodeWorkflowRecord == null){
        jMenuItem1.setEnabled(true);
        jMenuItem2.setEnabled(false);
        jMenuItem3.setEnabled(false);
        jMenuItem4.setEnabled(false);   
    }
    // workflow record
    else
    {
        jMenuItem1.setEnabled(true);
        if("P".equals(currentSelectedTreeNodeWorkflowRecord.getStatus()))
            jMenuItem2.setEnabled(true);        
        jMenuItem3.setEnabled(true);
        jMenuItem4.setEnabled(true);
    }
  }

  
  /**
   * reloadTree
   * reload Tree // not realization yet
   * 
   */  
  public void reloadTree(){
    int row = tree.getRowCount();
    for(int i=1;i<row+1;i++){
      tree.collapseRow(i);
    }
    int selectRow = tree.getLeadSelectionRow();
    tree = createGroupTree();
    tree.expandRow(selectRow);
    tree.addMouseListener( new MouseAdapter(){
        public void mousePressed(MouseEvent e){
            if(SwingUtilities.isRightMouseButton(e)){ 
                treePopupMenu.show(tree,e.getX(),e.getY()) ;
            }
        }    
    });
    sTree.getViewport().add(tree);

  }
  
  public Editor getEditor(){
    return this.editor; 
  }

  /**
   * getWorkflowStepList
   * get workflow step list by selected workflow record
   * 
   */    
  public List getWorkflowStepList(){
    
    ObjectInputStream inputFromServlet = null;   

    List workflowStepList = new ArrayList();  
    try{       

      inputFromServlet = httpOperation.getObjectInputStreamFromServlet(getStepDataURL
            +currentSelectedTreeNodeWorkflowRecord.getID());
     //System.out.println("inputFromServlet"+getStepDataURL+currentSelectedTreeNodeWorkflowRecord.getID());
      Object obj = null;
      obj = inputFromServlet.readObject();

      if (obj instanceof List) {
          workflowStepList = (List) obj;
      } else if (obj instanceof Exception) {
          //otherwise type cast it to Exception and throw that exception.
          throw (Exception) obj;
      }      
    }catch (Exception e){
        JOptionPane.showInternalMessageDialog(this,"Get workflow step record failed!","Message",JOptionPane.INFORMATION_MESSAGE);        
        return null;
    } 
    
    return workflowStepList;
  
  }
  
  /**
   * destroy
   * 
   */  
  public void destroy(){
    httpOperation = null; 
  }
  public URL getURLByString(String urlString){
    URL url = null;
        
    try{
    //URL openUrl = getURLByString("gif\\Open.gif");
        url =  new URL(baseURL, "/"+projectName+"/jsp/workflow/client/"+urlString);
    }catch(Exception e){
        System.out.println("Get Icon Error!") ;
    }    
    return url;        
  }
  
  public void drop(DropTargetDropEvent e) {
    try {
      DataFlavor stringFlavor = DataFlavor.stringFlavor;
      Transferable tr = e.getTransferable();

      if(e.isDataFlavorSupported(stringFlavor)) {
        String filename = 
            (String)tr.getTransferData(stringFlavor);

        if(!Utility.isEmpty(currentSelectedTreeNodeWorkflowRecord)){
          editor.closeEditor();
          editor.setWorkflowRecordID(currentSelectedTreeNodeWorkflowRecord.getID());
          //editor.setWorkflowStepList(getWorkflowStepList());                    
          editor.initEditor();
          editor.setEnabled(true);
          e.dropComplete(true);
        }
        e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
        
      }
      else {
        e.rejectDrop();
      }
    }
    catch(IOException ioe) {
      ioe.printStackTrace();
    }
    catch(UnsupportedFlavorException ufe) {
      ufe.printStackTrace();
    }
  }
  public void dragEnter(DropTargetDragEvent e) { }
  public void dragExit(DropTargetEvent e) { }
  public void dragOver(DropTargetDragEvent e) { }
  public void dropActionChanged(DropTargetDragEvent e) { }
}


  /**
   * customTreeCell (not use yet)
   * 
   */   
class customTreeCell extends DefaultTreeCellRenderer {
    //Border border = new EmptyBorder(1, 1, 1, 1);

    public customTreeCell() {
        setOpaque(true);
        //setBorder(border);
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
                                                  boolean sel,
                                                  boolean expanded,
                                                  boolean leaf, int row,
                                                  boolean hasFocus) {
        
        if( ((DefaultMutableTreeNode)value).getUserObject() instanceof TreeUseWorkflowRecord){            
            if("R".equals(((TreeUseWorkflowRecord)((DefaultMutableTreeNode)value).getUserObject()).getStatus())){
                this.setIcon(new ImageIcon("editor\\gif\\insert.gif"));
            }     
        }else if( ((DefaultMutableTreeNode)value).getUserObject() instanceof TreeUseWorkflowGroup){
                                 
        }else {
                                 
        }
        
        super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);

        setText(value.toString());
        if (sel)
            setForeground(getTextSelectionColor());
        else
            setForeground(getTextNonSelectionColor());
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;    
        
        String str = value.toString();
        if (str.equals("a") ) {
            this.setIcon(new ImageIcon("Add.gif"));
        }
        if (str.equals("b")) {
            this.setIcon(new ImageIcon("Copy.gif"));
        }
        if (str.equals("c")) {
            this.setIcon(new ImageIcon("Delete.gif"));
        }
        return this;
    }
}


      
